//
//  STNewOrderLogisticsView.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/27.
//

import UIKit

class STNewOrderLinkView: UIView {
    
    var type: Int
    
    override init(frame: CGRect) {
        self.type = 0
        super.init(frame: frame)
    }

    convenience init(type: Int ) {
        self.init(frame: CGRect.zero)
        self.type = type
        
        let line: UIView = UIView.init()
        line.layer.cornerRadius = 1
        line.backgroundColor = UIColor.init(hexString:"#008CFF", alpha: 1)
        addSubview(line)
        line.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(2)
        }
        
        let circle: UIView = UIView.init()
        circle.layer.cornerRadius = 4
        circle.backgroundColor = UIColor.init(hexString:"#008CFF", alpha: 1)
        circle.layer.borderWidth = 2
        circle.layer.borderColor = UIColor.white.cgColor
        addSubview(circle)
        circle.snp.makeConstraints { (make) in
            make.center.equalTo(line)
            make.width.equalTo(8)
            make.height.equalTo(8)
        }
        
        if(type == 1) {
            addSubview(arrowLabel)
            arrowLabel.snp.makeConstraints { (make) in
                make.centerY.equalTo(line.snp.centerY).offset(-1)
                make.right.equalTo(2)
            }
        }
    }

    lazy var arrowLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 16)
        lb.textColor = UIColor.init(hexString:"#008CFF", alpha: 1)
        lb.text = ">"
        return lb
    }()

    required init?(coder aDecoder: NSCoder) {
        self.type = 0
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}

class STNewOrderLogisticsDescView: UIView {
    
    var type: Int
    
    override init(frame: CGRect) {
        self.type = 0
        super.init(frame: frame)
    }

    convenience init(type: Int ) {
        self.init(frame: CGRect.zero)
        self.type = type
        
        layer.cornerRadius = 4
        
        addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.top.equalTo(2)
            make.left.equalTo(5)
            make.right.equalTo(-5)
            make.bottom.equalTo(-2)
        }
        
        if(type == 2) {
            backgroundColor = UIColor.init(hexString:"#FA951E", alpha: 0.2)
            descLabel.textColor = UIColor.init(hexString:"#FA951E", alpha: 1)
            descLabel.text = "目的地"
        } else {
            backgroundColor = UIColor.init(hexString:"#008CFF", alpha: 0.2)
            descLabel.textColor = UIColor.init(hexString:"#008CFF", alpha: 1)
            descLabel.text = type == 0 ? "提车港" : "装货港"
        }
    }
    
    lazy var descLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 13)
        return lb
    }()

    required init?(coder aDecoder: NSCoder) {
        self.type = 0
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}

class STNewOrderLogisticsView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(vehicleLabel)
        vehicleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(2)
            make.left.equalTo(20)
            make.height.equalTo(25)
        }
        
        addSubview(vehicleView)
        vehicleView.snp.makeConstraints { (make) in
            make.top.equalTo(vehicleLabel.snp.bottom).offset(2)
            make.centerX.equalTo(vehicleLabel)
        }
        
        addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(vehicleLabel.snp.right).offset(10)
            make.centerY.equalTo(vehicleLabel)
            make.width.equalTo(24)
            make.height.equalTo(12)
        }
        
        addSubview(loadingLabel)
        loadingLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(vehicleLabel)
            make.left.equalTo(lineView.snp.right).offset(10)
            make.height.equalTo(vehicleLabel)
        }
        
        addSubview(loadingView)
        loadingView.snp.makeConstraints { (make) in
            make.top.equalTo(vehicleView)
            make.centerX.equalTo(loadingLabel)
        }
        
        addSubview(arrowView)
        arrowView.snp.makeConstraints { (make) in
            make.left.equalTo(loadingLabel.snp.right).offset(10)
            make.centerY.equalTo(loadingLabel)
            make.width.equalTo(24)
            make.height.equalTo(20)
        }
    
        addSubview(targetLabel)
        targetLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(loadingLabel)
            make.left.equalTo(arrowView.snp.right).offset(10)
            make.height.equalTo(loadingLabel)
        }
        
        addSubview(targetView)
        targetView.snp.makeConstraints { (make) in
            make.top.equalTo(vehicleView)
            make.centerX.equalTo(targetLabel)
        }
        
        addSubview(stateLabel)
        stateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(targetLabel)
            make.right.equalTo(-18)
            make.height.equalTo(targetLabel)
        }
    }
    
    public func updateData(_ data: Dictionary<String, Any>) {
        vehicleLabel.text = (data["pickUpFrom"] as? String) ?? "--"
        loadingLabel.text = (data["loadFrom"] as? String) ?? "--"
        targetLabel.text = (data["finishFrom"] as? String) ?? "--"
        stateLabel.text = (data["statusName"] as? String) ?? "--"
    }
  
    lazy var stateLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 16)
        lb.textColor = UIColor.init(hexString:"#8F92A1", alpha: 1)
        return lb
    }()
    
    lazy var vehicleLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 18)
        lb.textColor = UIColor.init(hexString:"#008CFF", alpha: 1)
        return lb
    }()
    
    lazy var vehicleView: STNewOrderLogisticsDescView = {
        let v = STNewOrderLogisticsDescView.init(type: 0)
        return v
    }()
    
    lazy var lineView: STNewOrderLinkView = {
        let v = STNewOrderLinkView.init(type: 0)
        return v
    }()

    lazy var loadingLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 18)
        lb.textColor = UIColor.init(hexString:"#008CFF", alpha: 1)
        return lb
    }()
    
    lazy var loadingView: STNewOrderLogisticsDescView = {
        let v = STNewOrderLogisticsDescView.init(type: 1)
        return v
    }()
    
    lazy var arrowView: STNewOrderLinkView = {
        let v = STNewOrderLinkView.init(type: 1)
        return v
    }()
    
    lazy var targetLabel: UILabel = {
        let lb = UILabel.init()
        lb.font = UIFont.systemFont(ofSize: 18)
        lb.textColor = UIColor.init(hexString:"#FA951E", alpha: 1)
        return lb
    }()
    
    lazy var targetView: STNewOrderLogisticsDescView = {
        let v = STNewOrderLogisticsDescView.init(type: 2)
        return v
    }()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
